import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import svgLoader from "vite-svg-loader";

const mode = process.env.VITE_TITLE || "development";
const env = loadEnv(mode, process.cwd());

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: ["vue", "pinia"],
      dts: true,
    }),
    svgLoader({
      svgoConfig: {
        plugins: [
          // 1. 移除所有 fill/stroke 属性（排除 fill="none"）
          {
            name: "removeAttrs",
            params: {
              attrs: "(fill|stroke)",
            },
          },
          // 2. 添加 fill="currentColor" 到所有 path（排除 fill="none" 的元素）
          {
            name: "addAttributesToSVGElement",
            params: {
              attributes: [{ fill: "currentColor" }],
            },
          },
        ],
      },
    }),
  ],
  server: {
    port: Number(env.VITE_PORT),
    proxy: {
      [env.VITE_API_PREFIX]: {
        target: env.VITE_API_BASE_URL,
        changeOrigin: false,
        rewrite: (path) =>
          path.replace(new RegExp(`^${env.VITE_API_PREFIX}`), ""),
      },
      [env.VITE_WS_PREFIX]: {
        target: env.VITE_WS_BASE_URL,
        ws: true, // vite是默认ws:为true，此处可以不加
      },
    },
  },
  resolve: {
    alias: {
      "@": "/src",
    },
  },
});
